home *** CD-ROM | disk | FTP | other *** search
- { pmrun.pas -- Run the Program Manager via DDE link }
-
- program PMRun;
-
- {$R pmrun.res}
-
- uses WinTypes, WinProcs, WObjects, Strings, StdDlgs;
-
- const
-
- id_Menu = 100; { Menu resource ID }
- cm_CreateGroup = 101; { Create group command ID }
- cm_AddItem = 102; { Add item command ID }
- cm_ShowGroup = 103; { Show group command ID }
- cm_ExitWindows = 104; { Exit Windows command ID }
- cm_Quit = 105; { Exit program command ID }
- maxBufLen = 80; { Maximum size of a command string }
-
- serverName = 'PROGMAN'; { Program manager DDE server name }
- serverTopic = serverName; { Topic is the same as the app name }
-
- type
-
- PMRunApplication = object(TApplication)
- procedure InitMainWindow; virtual;
- end;
-
- PPMRunWindow = ^PMRunWindow;
- PMRunWindow = object(TWindow)
- LinkEstablished: Boolean; { True if conversation established }
- HWndPM: HWnd; { Handle to PM window }
- constructor Init(AParent: PWindowsObject; ATitle: PChar);
- function CanClose: Boolean; virtual;
- function GetCommand(Prompt, Buffer: PChar): Boolean;
- function Linked: Boolean;
- procedure SendPMCommand(P: PChar);
- procedure Execute(Prompt, Command: PChar);
- procedure WMDDEAck(var Msg: TMessage);
- virtual wm_First + wm_DDE_Ack;
- procedure WMDDETerminate(var Msg: TMessage);
- virtual wm_First + wm_DDE_Terminate;
- procedure CMCreateGroup(var Msg: TMessage);
- virtual cm_First + cm_CreateGroup;
- procedure CMAddItem(var Msg: TMessage);
- virtual cm_First + cm_AddItem;
- procedure CMShowGroup(var Msg: TMessage);
- virtual cm_First + cm_ShowGroup;
- procedure CMExitWindows(var Msg: TMessage);
- virtual cm_First + cm_ExitWindows;
- procedure CMQuit(var Msg: TMessage);
- virtual cm_First + cm_Quit;
- end;
-
-
- { PMRunApplication }
-
- {- Initialize PMRunApplication object's window }
- procedure PMRunApplication.InitMainWindow;
- begin
- MainWindow := New(PPMRunWindow, Init(nil, 'PMRun'))
- end;
-
-
- { PMRunWindow }
-
- {- Construct PMRunWindow object }
- constructor PMRunWindow.Init(AParent: PWindowsObject; ATitle: PChar);
- begin
- TWindow.Init(AParent, ATitle);
- Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
- LinkEstablished := false
- end;
-
- {- Unlink from PM and return true if application can end }
- function PMRunWindow.CanClose: Boolean;
- begin
- if LinkEstablished and IsWindow(HWndPM) then
- PostMessage(HWndPM, wm_DDE_Terminate, HWindow, 0);
- CanClose := true
- end;
-
- {- Prompt for cmd. Return Buffer and true or false to cancel }
- function PMRunWindow.GetCommand(Prompt, Buffer: PChar): Boolean;
- begin
- Buffer[0] := #0; { Erase buffer }
- GetCommand :=
- (Application^.ExecDialog(New(PInputDialog,
- Init(@Self, 'Program Manager Command', Prompt,
- Buffer, maxBufLen + 1))) = id_Ok) and (StrLen(Buffer) > 0)
- end;
-
- {- Respond to wm_DDE_Ack message from server }
- procedure PMRunWindow.WMDDEAck(var Msg: TMessage);
- var
- AppAtom, AppTopic: TAtom; { Atoms for wm_DDE_Initiate }
- HMem: THandle; { Memory handle for wm_DDE_Execute }
- DDEStatus: Word; { Execution status }
- begin
- if not LinkEstablished then
- begin
- LinkEstablished := true;
- AppTopic := HiWord(Msg.LParam);
- AppAtom := LoWord(Msg.LParam);
- HWndPM := Msg.WParam; { Save server's window handle }
- if AppAtom <> 0 then
- GlobalDeleteAtom(AppAtom);
- if AppTopic <> 0 then
- GlobalDeleteAtom(AppTopic)
- end else
- begin
- DDEStatus := LoWord(Msg.LParam);
- if (DDEStatus and dde_Ack) <> dde_Ack then
- MessageBox(HWindow,
- 'Command rejected by Program Manager', 'Error', mb_Ok);
- HMem := HiWord(Msg.LParam);
- if HMem <> 0 then
- GlobalFree(HMem)
- end;
- end;
-
- {- Respond to wm_DDE_Terminate message }
- procedure PMRunWindow.WMDDETerminate(var Msg: TMessage);
- begin
- LinkEstablished := false
- end;
-
- {- Link to PM if not linked. Return true if linked }
- function PMRunWindow.Linked: Boolean;
- var
- AppAtom, AppTopic: TAtom;
- begin
- if not LinkEstablished then
- begin
- AppAtom := GlobalAddAtom(serverName);
- AppTopic := GlobalAddAtom(serverTopic);
- SendMessage(Word(-1), wm_DDE_Initiate, HWindow,
- MakeLong(AppAtom, AppTopic));
- GlobalDeleteAtom(AppAtom);
- GlobalDeleteAtom(AppTopic)
- end;
- Linked := LinkEstablished
- end;
-
- {- Send command to Program Manager }
- procedure PMRunWindow.SendPMCommand(P: PChar);
- var
- HCmd: THandle; { Handle to memory block }
- PCmd: PChar; { Pointer to same block as a string }
- begin
- if Linked then
- begin
- HCmd := GlobalAlloc(gmem_Moveable or gmem_DDEShare,
- StrLen(P) + 1);
- if HCmd <> 0 then
- begin
- PCmd := GlobalLock(HCmd);
- if PCmd = nil then
- GlobalFree(HCmd)
- else begin
- StrCopy(PCmd, P);
- GlobalUnlock(HCmd);
- if not PostMessage(HWndPM, wm_DDE_Execute, HWindow,
- MakeLong(0, HCmd)) then
- GlobalFree(HCmd)
- end
- end
- end else
- MessageBox(HWindow, 'Link to Program Manager failed',
- 'Note', mb_Ok)
- end;
-
- {- Execute Program Manager command }
- procedure PMRunWindow.Execute(Prompt, Command: PChar);
- var
- P: PChar; { Pointer to locally allocated string }
- Len: Integer; { Length of full command string }
- Buffer: array[0 .. maxBufLen] of Char;
- begin
- if GetCommand(Prompt, Buffer) then
- begin
- Len := 1 {null} + 2 {brackets} + StrLen(Command) + 2 {parens} +
- StrLen(Buffer);
- GetMem(P, Len);
- if P = nil then
- MessageBox(HWindow, 'Out of memory',
- 'Error', mb_Ok)
- else begin
- StrCopy(P, '['); { Put command into correct format }
- StrCat(P, Command);
- StrCat(P, '(');
- StrCat(P, Buffer);
- StrCat(P, ')]');
- SendPMCommand(P); { Send command to PM }
- FreeMem(P, Len)
- end
- end
- end;
-
- {- Create new program manager group window }
- procedure PMRunWindow.CMCreateGroup(var Msg: TMessage);
- begin
- Execute('GroupName[,GroupPath]', 'CreateGroup')
- end;
-
- {- Add program item to a group window }
- procedure PMRunWindow.CMAddItem(var Msg: TMessage);
- begin
- Execute('CmdLine[,Name[,IconPath[,IconIndex[,X,Y]]]]', 'AddItem')
- end;
-
- {- Show group window if it exists }
- procedure PMRunWindow.CMShowGroup(var Msg: TMessage);
- begin
- Execute('GroupName,Command', 'ShowGroup')
- end;
-
- {- Exit Program Manager, and subsequently, Windows }
- procedure PMRunWindow.CMExitWindows(var Msg: TMessage);
- var
- P: PChar;
- begin
- SendPMCommand('[ExitProgman(TRUE)]') { ??? }
- end;
-
- {- Exit this application only }
- procedure PMRunWindow.CMQuit(var Msg: TMessage);
- begin
- CloseWindow
- end;
-
- var
-
- PMRunApp: PMRunApplication;
-
- begin
- PMRunApp.Init('PMRunApp');
- PMRunApp.Run;
- PMRunApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 5/28/1991
- ---------------------------------------------------------------}
-